package cn.imvc.app.models;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.IndexClass;
import cn.imvc.app.repositories.IndexClassRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class IndexClassModelImpl implements IndexClassModel {

	@Autowired
	IndexClassRepository indexClassRepository;
	@Autowired
	RefRelationModel refRelationModel;
	
	public Page<IndexClass> find(ViewRequest viewRequest) {
		return indexClassRepository.findAll("i","IndexClass i",ModelUtil.OrderByIdDesc(viewRequest));
	}

	public IndexClass get(int id) {
		return indexClassRepository.findOne(id);
	}

	public IndexClass save(IndexClass indexClass) {
		return indexClassRepository.saveAndFlush(indexClass);
	}

	public IndexClass update(int id,IndexClass indexClass) {
		IndexClass loadIndexClass = indexClassRepository.getOne(id);
		BeanUtils.copyProperties(indexClass, loadIndexClass, new String[]{"id"});
		return indexClassRepository.saveAndFlush(loadIndexClass);
	}

	public String delete(int id) {
		String result = refRelationModel.deleteDateCheck("Index_Class", "id", id);
		if(result.equals("")){
			indexClassRepository.delete(id);
			return "";
		}else{
			return result;
		}
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}

}
